/**
 * 
 */
package com.swim.coach.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.swim.coach.entity.Coach;
import com.swim.coach.service.CoachService;
import com.swim.common.utils.SmsUtil;

/**
 * 私教Controller
 * @author 刘军武
 * @version 2021-05-06
 */
@Controller
@RequestMapping(value = "${adminPath}/coach/coach")
public class CoachController extends BaseController {

	@Autowired
	private CoachService coachService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Coach get(String id, boolean isNewRecord) {
		return coachService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("coach:coach:view")
	@RequestMapping(value = {"list", ""})
	public String list(Coach coach, Model model) {
		model.addAttribute("coach", coach);
		return "swim/coach/coachList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("coach:coach:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Coach> listData(Coach coach, HttpServletRequest request, HttpServletResponse response) {
		Page<Coach> page = coachService.findPage(new Page<Coach>(request, response), coach); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("coach:coach:view")
	@RequestMapping(value = "form")
	public String form(Coach coach, Model model) {
		model.addAttribute("coach", coach);
		return "swim/coach/coachForm";
	}

	/**
	 * 保存私教
	 */
	@RequiresPermissions("coach:coach:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Coach coach) {
		coachService.save(coach);
		try {
			//私教入驻发送短信给李教练
			SmsUtil.sendCoachByLi("13912785198", coach.getName(), coach.getMobile());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return renderResult(Global.TRUE, text("保存私教成功！"));
	}
	
	/**
	 * 删除私教
	 */
	@RequiresPermissions("coach:coach:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Coach coach) {
		coachService.delete(coach);
		return renderResult(Global.TRUE, text("删除私教成功！"));
	}

	/**
	 * 查看详情表单
	 */
	@RequiresPermissions("coach:coach:view")
	@RequestMapping(value = "details")
	public String details(Coach coach, Model model) {
		model.addAttribute("coach", coach);
		return "swim/coach/coachDetails";
	}
	
}